#include "addaudio_batches.h"
#include "ui_addaudio_batches.h"
#include<QFileDialog>
#include<qobject.h>
#include<qdebug.h>

AddAudio_batches::AddAudio_batches(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AddAudio_batches)
{
    ui->setupUi(this);
    this->setWindowTitle("为视频添加音频");
    connect(&yiping,&addyiping::signal_addyinping_over,this,&AddAudio_batches::slot_addyinping_over);

    connect(&this->timer,&QTimer::timeout,this,&AddAudio_batches::slot_timer);

    connect(&yiping,&addyiping::signal_outtext,this,&AddAudio_batches::slot_outtext);
}

AddAudio_batches::~AddAudio_batches()
{
    delete ui;
}

void AddAudio_batches::addyinping()
{
    if(next>5)
    {
        next=0;
    }
    qDebug()<<"next="<<next;

    if(shiping[0]==nullptr&&shiping[1]==nullptr&&shiping[2]==nullptr&&shiping[3]==nullptr&&shiping[4]==nullptr&&shiping[5]==nullptr)
    {
        ui->plainTextEdit->setPlainText("任务列表为空...");
        ui->shipin1->clear();
        ui->shipin1_2->clear();
        ui->shipin1_3->clear();
        ui->shipin1_4->clear();
        ui->shipin1_5->clear();
        ui->shipin1_6->clear();

        ui->yinping1->clear();
        ui->yinping1_2->clear();
        ui->yinping1_4->clear();
        ui->yinping1_3->clear();
        ui->yinping1_5->clear();
        ui->yinping1_6->clear();

        ui->pushButton->setEnabled(true);
        return;
    }

    if(shiping[next]==nullptr)
    {
        next++;
        addyinping();
        return;
    }

    QString yinp_outfilename(shiping[next]);
    yinp_outfilename=yinp_outfilename.insert(yinp_outfilename.length()-4,"精听");

    ui->plainTextEdit->appendPlainText("开始制作视频...");
    yiping.Addyinping(shiping[next],yinping[next],yinp_outfilename);qDebug()<<shiping[next]<<yinping[next];

    return;
}

bool AddAudio_batches::DeleteFileOrFolder(const QString &strPath)
{
    if (strPath.isEmpty() || !QDir().exists(strPath))//是否传入了空的路径||路径是否存在
        return false;

    QFileInfo FileInfo(strPath);

    if (FileInfo.isFile())//如果是文件
        QFile::remove(strPath);
    else if (FileInfo.isDir())//如果是文件夹
    {
        QDir qDir(strPath);
        qDir.removeRecursively();
    }
    return true;
}

void AddAudio_batches::on_pushButton_clicked()
{
    //this->outfilename=QFileDialog::getOpenFileName(this,"选择保存路径");


    this->shiping[0]=ui->shipin1->toPlainText();
    this->shiping[1]=ui->shipin1_2->toPlainText();
    this->shiping[2]=ui->shipin1_3->toPlainText();
    this->shiping[3]=ui->shipin1_4->toPlainText();
    this->shiping[4]=ui->shipin1_5->toPlainText();
    this->shiping[5]=ui->shipin1_6->toPlainText();

    for(int i=0;i<6;i++)
    {
        if(shiping[i].left(8)=="file:///")
        {
            shiping[i]=shiping[i].right(shiping[i].length()-8);
        }
    }

    this->yinping[0]=ui->yinping1->toPlainText();
    this->yinping[1]=ui->yinping1_2->toPlainText();
    this->yinping[2]=ui->yinping1_3->toPlainText();
    this->yinping[3]=ui->yinping1_4->toPlainText();
    this->yinping[4]=ui->yinping1_5->toPlainText();
    this->yinping[5]=ui->yinping1_6->toPlainText();

    for(int i=0;i<6;i++)
    {
        if(yinping[i].left(8)=="file:///")
        {
            yinping[i]=yinping[i].right(yinping[i].length()-8);
        }
    }

    next=0;
    this->addyinping();
    ui->pushButton->setEnabled(false);
    ui->plainTextEdit->appendPlainText("开始制作...");
}

void AddAudio_batches::slot_addyinping_over()
{
    ui->plainTextEdit->appendPlainText("已添加一个音频,正在检查任务列表...");


    before=next;
    next++;
    //删除源文件
    this->DeleteFileOrFolder(shiping[before]);
    ui->plainTextEdit->appendPlainText("已删除原视频...");
    shiping[before].clear();yinping[before].clear();

    qDebug()<<"void AddAudio_batches::slot_addyinping_over()";

    this->timer.start(8000);
    //this->addyinping();

    return;
}

void AddAudio_batches::slot_timer()
{
    qDebug()<<"void AddAudio_batches::slot_timer()";
    this->timer.stop();
    this->addyinping();
}

void AddAudio_batches::slot_outtext(QString str)
{

    qDebug()<<"void AddAudio_batches::slot_outtext(QString str)";
    ui->plainTextEdit->appendPlainText(str);
}

